package Reunion::User::Resource;
use Mojo::Base 'Mojolicious::Controller';

use Reunion::User::Model::Resource;

#our $resource = Reunion::User::Model::Resource->new();

sub index {
    my $self = shift;
    my $resource = Reunion::User::Model::Resource->new($self->dbi);
    my $result = $resource->getResourceList();
    $self->render('rlist' => $result->all);
}

sub add {
    my $self = shift;

    my $username = $self->param('user_name');
    my $password = $self->param('password');
    my $rememberMe = $self->param('login');

    my $user = Reunion::User::Model::User->new($self->dbi);
    my $result = $user->login($username, $password);
    my $row = $result->one;

    if ($row) {
        my $userInfo = {
                       'user_id'=>$row->{'user_id'},
                       'user_name'=>$row->{'user_name'},
                       'user_lang'=>$row->{'user_lang'},
                       'user_status'=>$row->{'user_status'},
                       'user_type'=>$row->{'user_type'},
                       'user_owner'=>$row->{'user_owner'},
                       'user_group'=>$row->{'user_group'},
                       'user_company'=>$row->{'user_company'}
                       };
        $self->session->{'userInfo'} = $userInfo;
        $self->session->{'defaultLanguage'} = $row->{'user_lang'};
        $self->redirect_to('/');
    } else {
        $self->flash(msg => 'User Forbidden!');
        $self->redirect_to('/login');
    }
}

sub edit {
    my $self = shift;
    $self->redirect_to('/resource');
}

1;
